package com.Akronmcp.hta.obj;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.entity.Player;

import com.Akronmcp.hta.main.HLoader;
import com.Akronmcp.hta.main.HSender;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**插件主引擎,用来储存对象,方法,配置*/
public class HManager {
	
	public static final Map<String,HopChat> hchatMap = new HashMap<String,HopChat>();
	public static final Map<String,HopTitle> htitleMap = new HashMap<String,HopTitle>();
	public static final Map<String,HopBar> hbarMap = new HashMap<String,HopBar>();
	public static final Map<String,HopBook> hbookMap = new HashMap<String,HopBook>();
	public static final Map<String,HopHolo> hholoMap = new HashMap<String,HopHolo>();
	//储存各类实例
	
	public static final
	Map<String, HPar2> htmlist = new HashMap<String,HPar2>();
	
	public static final List<HManager> HTAList = new ArrayList<HManager>();
	
	static
	{
		
	}
	
 	public HManager()
	{
 		HTAList.add(this);
	}
 	

 	/**为hopchat生成json*/
	public void createToJson(HopChat o)
	{
		String str1 = "§r"+o.getText();
		String caction = o.getCaction();
		String cvalue = o.getCvalue();
		String haction = o.getHaction();
		String hvalue = o.getHvalue();
		
	
		JsonArray jsonArray = new JsonArray();
		//定义json数组
		
		String stra1[] = str1.split("</");
		List<String> strl1 = new ArrayList<>();
		String stra2[] = null ;
		
		for(String str2:stra1)
		{
			
		  String tstra1[] = str2.split("/>");
		  if(tstra1.length > 1)//提取有}的,处理
		  {
			  tstra1[0] = tstra1[0];
			  //添加标识
			  strl1.add(tstra1[0]);
			  strl1.add(tstra1[1]);
		  }
		  else
		  {
			  strl1.add(tstra1[0]);
		  }
		}//哲学分析引用符{}
		
		
		   stra2 = strl1.toArray(new String[0]);
		  //由处理结果生成子文本数组
		   
	f1: for(String partstr:stra2)
		{//判断每一个子文本
			//System.out.println(partstr +"sss0");
			if(partstr.equals(""))continue;
			//过滤空文本
			
			if(partstr.startsWith("@"))
			{//识别引用符
				
			  partstr = partstr.substring(1);
			  //去除引用符
			  HopChat yhtx = hchatMap.get(partstr);
			  if(yhtx!=null)
			  {
				  if(!yhtx.isCreated())
					  yhtx.create();
					  
				  jsonArray.add(yhtx.getJson());
				  
			  }else System.out.println("未找到:partstr");
			 
			  continue f1;
			  
			}else if(partstr.startsWith("="))
			{//处理即时创建符
				//System.out.println(partstr +"sss2");
			    partstr = partstr.substring(1);
				String targ[] = partstr.split(";\\n{0,1}");
				//通过;分割参数
					
				JsonObject jo = new JsonObject();
				jo.addProperty("text", targ[0]);
				//参数1  显示文本
				
				if(targ.length>1)
				{//处理参数2 点击事件
				 JsonObject jocl = new JsonObject();
				
					 String v = null;
					 String a = null;
					 
					 if(targ[1].length()>4)
					 {//识别前缀,如run:
						 v = targ[1].substring(4);
						 a = targ[1].substring(0,4);
					 }else
					 {
						 v = targ[1];
					 }
					 
					 String ac = "run_command";
					//默认点击事件为
					 
					if(a!=null)
					switch(a)
					{
					case "run:":
					ac= "run_command";
					break;
					
					case "url:" :
					ac = "open_url";
					break;
					
					case "sug:" :
					ac = "suggest_command";
					break;
					
					case "cha:" :
					ac = "change_page";
					break;
					
					default:
				    v = targ[1];
					}
					
				     jocl.addProperty("action", ac);
					 jocl.addProperty("value", v);
				     jo.add("clickEvent",jocl);
				 
				 
				if(targ.length>2) 
				{//处理参数3  显示文本
				 JsonObject joho = new JsonObject();
				 joho.addProperty("action", "show_text");
				 joho.addProperty("value", targ[2]);
				 
				 jo.add("hoverEvent",joho);
				}
				
				
				 
				}
					
				
				
				jsonArray.add(jo);
				continue f1;
				
			}
			
			        //_处理非引用子文本,加入json数组
				    JsonObject jo = new JsonObject();
					jo.addProperty("text", partstr);
					//System.out.println(partstr +"sss2");
					
					if(caction!=null)
					{
					 JsonObject jocl = new JsonObject();
					 jocl.addProperty("action", caction);
					 jocl.addProperty("value", cvalue);
					 
					 jo.add("clickEvent",jocl);
					}
						
					if(haction!=null)
					{
					 JsonObject joho = new JsonObject();
					 joho.addProperty("action", haction);
					 joho.addProperty("value", hvalue);
					 
					 jo.add("hoverEvent",joho);
					}
					
					jsonArray.add(jo);
   	
					
		}
		
		if(jsonArray!=null)
		{
			o.setJson(jsonArray);
			o.iscreated = true;
		}
		   
	} 

	/**加入HopTitle到list*/
	public void addTitle(HopTitle htl)
	{
		String id = htl.getId();
		if(id.equals(null))//排除空id
		    return;	
		
		htitleMap.put(id,htl);
	}

	public void addText(HopChat htx)
	{
		String id = htx.getId();
	    if(id.equals(null))//排除空id
	    return;	
		    
	    hchatMap.merge(id, htx,(o,n)->{
			  o.setJson(n.getJson());
			  o.setText(n.getText());
			  o.iscreated = true;
			  return o;
	    });
	}
	
	public void addBar(HopBar hb)
	{
		String id = hb.getId();
        if(id.equals(null))//排除空id
		    return;	
		
		hbarMap.put(id,hb);
	}
	public void addHolo(HopHolo hb)
	{
		String id = hb.getId();
        if(id.equals(null))//排除空id
		    return;	
		
		hholoMap.put(id,hb);
	}
	
	public void addBook(HopBook hb)
	{
		String id = hb.getId();
        if(id.equals(null))//排除空id
		    return;	
		
		hbookMap.put(id,hb);
	}
	
	public void addHopModule(String key,HPar2 hp)
	{
		htmlist.put(key, hp);
	}
	
	/**发送玩家一个hchat,若玩家为null则发送给所有人*/
	public boolean sendTellraw(String id,Player p)
	{
		 HopChat ht = hchatMap.get(id);
		 if(ht==null)
		 return false;
		 
		 if(p!=null)
		 {
			 ht.send(p);
		 }else 
		 {ht.sendToAll();}
		 return true;
	}
	
	
	public void sendTitle(HopTitle ht,Player p)
	{
        if(!ht.type.equals("ACTIONBAR"))
		HSender.sendTitle(p,null,"TIMES",ht.getFadein(),ht.getStayTime(),ht.getFadeout());
		
		HSender.sendTitle(p,ht.getTText(),ht.type);	
	}
	
	public boolean sendTitle(String id,Player p)
	{
		 HopTitle ht = htitleMap.get(id);
		 if(ht==null)
		 return false;
		 if(p!=null)
		 {
			 ht.send(p);
		 }else 
		 {ht.sendToAll();}
		 return true;
		
	}
	
	public boolean sendBar(String id,Player p)
	{
		 HopBar ht = hbarMap.get(id);
		 
		 if(ht==null)
		 return false;
		 if(p!=null)
		 {
			 ht.send(p);
		 }else 
		 {ht.sendToAll();}
		 return true;
	}
	
	public boolean givebook(String id,Player p)
	{
		 HopBook ht = hbookMap.get(id);
		 
		 if(ht==null)
		 return false;
		 
		 if(p!=null)
		 {
			 ht.give(p);
		 }else 
		 {ht.giveToAll();}
		 return true;
	}
	
	
	public HopChat getHopChat(String id)
	{
		return hchatMap.get(id);
	}
	
	public HopHolo getHopHolo(String id)
	{
		return hholoMap.get(id);
	}

	public HopTitle getHopTitle(String id)
	{
		return htitleMap.get(id);
	}
	
	public HopBar getHopBar(String id)
	{
		return hbarMap.get(id);
	}

	public HopBook getHopBook(String id)
	{
	    return hbookMap.get(id);
	}

	
	public void clearhtx()
	{new ArrayList<>(hchatMap.values()).forEach((o)->{if(o.hta.equals(this))hchatMap.remove(o.id);});}
	
	public void clearhtl()
	{new ArrayList<>(htitleMap.values()).forEach((o)->{if(o.hta.equals(this))htitleMap.remove(o.id);});}
	
	public void clearhtb()
	{new ArrayList<>(hbarMap.values()).forEach((o)->{if(o.hta.equals(this))hbarMap.remove(o.id);});}
	
	public void clearhto()
	{new ArrayList<>(hbookMap.values()).forEach((o)->{if(o.hta.equals(this))hbookMap.remove(o.id);});}
	
	public void clearhth()
	{
		new ArrayList<>(hholoMap.values()).forEach((o)->
		{
			if(o.hta.equals(this)) 
			{
			 hholoMap.remove(o.id);
			 o.delete();
			}
		});
	}
	
	
	public void clearAll()
	{
	  clearhtx();
	  clearhtl();
	  clearhtb();
	  clearhto();
	  clearhth();
	}
	
	public void initAllHopChat()
	{
		hchatMap.values().forEach((o)->{
			if(o.hta.equals(this))
			if(!o.isCreated())
			o.create();
		});
	}
	
	public void initAllHopBook()
	{
		hbookMap.values().forEach((o)->{
			if(o.hta.equals(this))
			o.createBook(null);
		});
	}
	
	public void initAllHopHolo()
	{
		hholoMap.values().forEach((o)->{
			if(o.hta.equals(this))
			if(o.location!=null)
			o.create();
		});
	}
	
	/**载入从*/
	public void load(File f)
	{
	   try 
	   {
		HLoader.load(this,f);
	   } catch (Exception e) 
	   {
		e.printStackTrace();
	   }
	}
	
}

